/************************
 * 
 * @author Justin Adams 
 * Student # : 200735090
 * 
 ***********************/

package tttCore;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

// a simple text reader class
public class TextReader {

	private ArrayList<String> lines = new ArrayList<String>();

	private final File fFile;
	private int currentLine = 1;

	public TextReader(String filePath){
		fFile = new File(filePath);  
	}

	public final void processLineByLine() throws FileNotFoundException {
		Scanner scanner = new Scanner(new FileReader(fFile));
		try {
			//first use a Scanner to get each line
			while ( scanner.hasNextLine() ){
				processLine( scanner.nextLine() );
				currentLine++;
			}
		}
		finally {
			scanner.close();
			currentLine = 1;
		}
	}

	protected void processLine(String aLine){
		//use a second Scanner to parse the content of each line 
		Scanner scanner = new Scanner(aLine);
		String line = "";
		scanner.useDelimiter(";");
		if ( scanner.hasNext() ){
			line = scanner.next();
		}else {
			log("Empty or invalid line. Unable to process.");
		}
		addToArray(line);
		//no need to call scanner.close(), since the source is a String
	}

	private static void log(Object aObject){
		System.out.println(String.valueOf(aObject));
	}

	private void addToArray(String line){
		lines.add(line);
	}
	
	public ArrayList<String> getLines(){
		try{
		processLineByLine();
		return lines;
		}catch(Exception e){
			System.out.println("txtReader Error");
			return lines;
		}
	}

} 

